package com.xue.springboot001.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@Controller
public class FileInputController {

    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("password") String password,
                         @RequestPart("headImg") MultipartFile headImg,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {
        log.info("上传的信息:email{},password{},headImg{},photos{}",email,password,headImg,photos);
        if (!headImg.isEmpty()){
            String originalFilename = headImg.getOriginalFilename();
            headImg.transferTo(new File("D:\\1\\"+originalFilename));
        }
        if (photos.length > 0){
            for (MultipartFile photo : photos) {
                if (!photo.isEmpty()){
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("D:\\1\\"+originalFilename));
                }
            }
        }
        return "main";
    }
}
